package com.ninthgroup.controller;

import com.ninthgroup.entity.Admin;
import com.ninthgroup.entity.News;
import com.ninthgroup.service.NewsService;
import com.ninthgroup.util.WebUtil;
import com.ninthgroup.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：Student王心
 * @date ：Created in 2022/10/14 15:26
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    /**
     * 根据新闻对象添加一条新闻
     * @param news
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody News news){
        System.out.println("news = " + news);
        return newsService.insertNews(news);
    }


    /**
     * 查询所有新闻信息并携带分页、新闻类别信息
     * @param currentPage
     * @param pageSize
     * @param news
     * @return
     */
    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "1")Integer currentPage,
                       @RequestParam(defaultValue = "2")Integer pageSize,
                       @RequestBody News news){
        return newsService.findAllNews(currentPage,pageSize,news);
    }


    /**
     * 根据新闻对象获取审核状态（通过）
     * @param news
     * @return
     */
    @RequestMapping("/updateStatus")
    public Result updateStatus(@RequestBody News news){
         Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");
         news.setNewsUser(admin.getaName());
        //news.setNewsUser("超级管理员");
        return newsService.updateNewsStatus(news);
    }

    /**
     * 根据新闻对象获取审核状态（未通过）
     * @param news
     * @return
     */
    @PostMapping("/updateAuditStatus")
    public Result updateAuditstatus(@RequestBody News news){
        return newsService.updateAuditStatus(news);
    }


    /**
     * 根据Id的查询拒绝理由
     * @param id
     * @return
     */
    @PostMapping("/queryReason")
    public Result queryReason(Integer id){
        return newsService.findById(id);
    }

    /**
     * 根据新闻对象修改新闻
     * @param news
     * @return
     */
    @PostMapping("/updateNews")
    public Result updateNews(@RequestBody News news){
        return newsService.updateNews(news);
    }


    /**
     * 根据当前页，页码，状态和置顶查询所有的新闻信息
     * @return
     */
    @RequestMapping("/queryNewsUppid")
    public Result queryNewsUppid(@RequestParam(defaultValue = "1")Integer currentPage,
                             @RequestParam(defaultValue = "5")Integer pageSize){
    return newsService.findByUppid(currentPage,pageSize);
    }



    /**
     * 客户端查询点击量进行排序
     * @return
     */
    @RequestMapping("/queryNewsClick")
    public Result queryNewsClick(){
        return newsService.findByNewsClick();
    }


    /**
     * 客户端查询置顶的新闻
     * @return
     */
    @RequestMapping("/lookNewsUppid")
    public Result lookNewsUppid(){
        return newsService.findByNewsUppid();
    }
}